package com.shijun.im.http;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * Created by Administrator on 2017/9/12 0012.
 */

public class CustomResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private Class clazz;
    public CustomResponseBodyConverter(Class<T> clazz){
        this.clazz = clazz;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {

        try {
            return (T)(new Gson().fromJson(value.string(),clazz));
        }catch (JsonParseException e){
            return null;
        }finally {
            value.close();
        }
    }
}